package com.jy.adscm.operation.hscrb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.hscrb.bean.ZjQualitycheck;
import com.jy.adscm.operation.hscrb.bean.ZjQualitycheck;
import com.jy.adscm.operation.hscrb.mapper.mysql.ZjQualitycheckMapper;
import com.jy.adscm.operation.hscrb.service.ZjQualitycheckService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-14
 */
@Service
public class ZjQualitycheckServiceImpl extends ServiceImpl<ZjQualitycheckMapper, ZjQualitycheck> implements ZjQualitycheckService {
@Autowired
    JyUserMapper jyUserMapper;
    @Override
    public Boolean updateZjQualitycheck(ZjQualitycheck zjQualitycheck, Token token) {
        //待办修改
        if (zjQualitycheck.getTaskstate()==0 && zjQualitycheck.getTaskfinishstate()==0 &&
                "0".equals(zjQualitycheck.getIstaskstart()) &&  "0".equals(zjQualitycheck.getIstaskbegin()) &&
                "0".equals(zjQualitycheck.getIstaskend())  && "0".equals(zjQualitycheck.getIsreviewtask())
        ){


            return  super.updateById(zjQualitycheck);
        }
        //接收任务
        if (zjQualitycheck.getTaskstate()==0 && zjQualitycheck.getTaskfinishstate()==0 &&
                "1".equals(zjQualitycheck.getIstaskstart()) &&  "0".equals(zjQualitycheck.getIstaskbegin()) &&
                "0".equals(zjQualitycheck.getIstaskend())  && "0".equals(zjQualitycheck.getIsreviewtask())
        ){
            zjQualitycheck.setIstaskstart("1");
            zjQualitycheck.setTaskstartaddtime(new Date());
            zjQualitycheck.setTaskstate(1);

            return  super.updateById(zjQualitycheck);
        }
        //开始任务
        if (zjQualitycheck.getTaskstate()==1 && zjQualitycheck.getTaskfinishstate()==0 &&
                "1".equals(zjQualitycheck.getIstaskstart()) &&  "1".equals(zjQualitycheck.getIstaskbegin()) &&
                "0".equals(zjQualitycheck.getIstaskend())  && "0".equals(zjQualitycheck.getIsreviewtask())
        ){
            zjQualitycheck.setIstaskbegin("1");
            zjQualitycheck.setTaskbeginaddtime(new Date());
            return super.updateById(zjQualitycheck);
        }
        //结束任务
        if (zjQualitycheck.getTaskstate()==1 && zjQualitycheck.getTaskfinishstate()==0 &&
                "1".equals(zjQualitycheck.getIstaskstart()) &&  "1".equals(zjQualitycheck.getIstaskbegin()) &&
                "1".equals(zjQualitycheck.getIstaskend())  && "0".equals(zjQualitycheck.getIsreviewtask())
        ){
            zjQualitycheck.setIstaskend("1");
            zjQualitycheck.setTaskfinishstate(1);
            zjQualitycheck.setTaskendaddtime(new Date());
            return super.updateById(zjQualitycheck);
        }
        //复核任务
        if (zjQualitycheck.getTaskstate()==1 && zjQualitycheck.getTaskfinishstate()==1 &&
                "1".equals(zjQualitycheck.getIstaskstart()) &&  "1".equals(zjQualitycheck.getIstaskbegin()) &&
                "1".equals(zjQualitycheck.getIstaskend())  && "1".equals(zjQualitycheck.getIsreviewtask())
        ){
            zjQualitycheck.setIsreviewtask("1");
            zjQualitycheck.setReviewaddtime(new Date());
            zjQualitycheck.setReviewperson(token.getUser().getId().toString());
            return super.updateById(zjQualitycheck);
        }

        return null;
    }

    @Override
    public List<ZjQualitycheck> selectZjQualitycheck(Integer taskState, Integer taskFinishState, Integer isReviewTask) {
        Wrapper<ZjQualitycheck> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",taskState);
        wrapper.eq("TASKFINISHSTATE",taskFinishState);
        wrapper.eq("isReviewTask",isReviewTask);
        List<ZjQualitycheck> zjQualitychecks= super.selectList(wrapper);



        for (ZjQualitycheck zjQualitycheck : zjQualitychecks) {
            if ( zjQualitycheck.getTaskreleaseperson()!=null && !"".equals( zjQualitycheck.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(zjQualitycheck.getTaskreleaseperson());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    zjQualitycheck.setTaskreleasepersonName(jyUser.getName());
                }else {
                    zjQualitycheck.setTaskreleasepersonName(zjQualitycheck.getTaskreleaseperson());
                }


            }
            if ( zjQualitycheck.getTaskperformperson()!=null && !"".equals( zjQualitycheck.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(zjQualitycheck.getTaskperformperson());

                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    zjQualitycheck.setTaskperformpersonName(jyUser.getName());
                }else {
                    zjQualitycheck.setTaskperformpersonName(zjQualitycheck.getTaskperformperson());
                }
            }

            if ( zjQualitycheck.getReviewperson()!=null && !"".equals( zjQualitycheck.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(zjQualitycheck.getReviewperson());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    zjQualitycheck.setReviewpersonName(jyUser.getName());
                }else {
                    zjQualitycheck.setReviewpersonName(zjQualitycheck.getReviewperson());
                }
            }

        }

        return zjQualitychecks;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("106");
        return jyUsers;
    }
}
